using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace courseSettings
{
    public partial class Settings : Form
    {
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        private string path;
        private string broj;

        public Settings()
        {
            InitializeComponent();
        }

        private void Settings_Load(object sender, EventArgs e)
        {
            // Otvori opcije.ini
            path = Application.StartupPath + "\\opcije.ini";
            StringBuilder temp = new StringBuilder(255);

            int i = GetPrivateProfileString("opcije", "broj", "", temp, 1024, path);
            broj = temp.ToString();

            // Petlja vadjenja kurseva iz opcije.ini
            for (int x = 0; x < Convert.ToInt32(broj); x++)
            {
                i = GetPrivateProfileString("kursevi", x.ToString(), "", temp, 1024, path);
                if (temp.ToString().StartsWith("1"))
                    checkedListBox1.Items.Add(temp.ToString().Remove(0, 2), true);
                else checkedListBox1.Items.Add(temp.ToString(), false);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            System.IO.File.Delete("opcije.ini");
            WritePrivateProfileString("opcije", "broj", checkedListBox1.Items.Count.ToString(), path);

            for (int i = 0; i < checkedListBox1.Items.Count; i++)
                WritePrivateProfileString("kursevi", i.ToString(), ((int)checkedListBox1.GetItemCheckState(i)).ToString() + ":" + checkedListBox1.Items[i].ToString(), path);

            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int br = Convert.ToInt32(broj) + 1;
            WritePrivateProfileString("kursevi",broj,"1:" + ime.Text+"|"+id.Text, path);
            WritePrivateProfileString("opcije","broj", br.ToString(), path);
            checkedListBox1.Items.Add(ime.Text + "|" + id.Text,true);
        }

        private void desniKlik_Opening(object sender, CancelEventArgs e)
        {

        }

        private void izbrisiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int br = Convert.ToInt32(broj) - 1;
            WritePrivateProfileString("kursevi", checkedListBox1.SelectedIndex.ToString(), null, path);
            checkedListBox1.Items.RemoveAt(checkedListBox1.SelectedIndex);
            WritePrivateProfileString("opcije", "broj", broj, path);
        }

    }
}